package com.ffcs.ffxtc.modules.base.dao.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import  com.ffcs.ffxtc.modules.base.domin.entity.UserInfo;
import com.ffcs.ffxtc.modules.base.dao.DrawdbUserService;
import com.ffcs.ffxtc.modules.base.dao.ShareDataService;
import com.ffcs.ffxtc.modules.base.domin.entity.DrawdbUser;
import com.ffcs.ffxtc.modules.base.domin.entity.ShareData;
import com.ffcs.ffxtc.modules.base.mapper.DrawdbUserMapper;
import com.ffcs.ffxtc.utils.UserUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 张正兴
 * @since 2024-04-30
 */
@Service
@RequiredArgsConstructor
public class DrawdbUserServiceImpl extends ServiceImpl<DrawdbUserMapper, DrawdbUser> implements DrawdbUserService {

    private final ShareDataService shareDataService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean share(List<ShareData> shareData) {
        UserInfo userInfo = UserUtils.getUserInfo();

        shareDataService.remove(new UpdateWrapper<ShareData>()
                .eq(ShareData.DRAWDB_USER_ID,  Long.parseLong(userInfo.getId()))
                .eq(ShareData.DRAWDB_DATA_ID, shareData.get(0).getDrawdbDataId())
        );

        shareData.forEach((data) -> {
            data.setDrawdbUserId(Long.parseLong(userInfo.getId()));
        });

        return shareDataService.saveBatch(shareData);
    }
}
